package org.apache.commons.graph.algorithms.coloring

import org.apache.commons.graph.model.UndirectedMutableGraph
import org.junit.Assert
import java.util.*

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * Abstract class used for test coloring.
 */
abstract class AbstractColoringTest {
    /**
     * This method checks if all connected vertices have different colors.
     *
     * @param g
     * @param coloredVertices
     */
    protected fun <V, E, C> checkColoring(
        g: UndirectedMutableGraph<V, E>,
        coloredVertices: ColoredVertices<V, C>
    ) {
        for (e in g.edges) {
            val vp = g.getVertices(e)
            val h = coloredVertices.getColor(vp!!.head)
            val t = coloredVertices.getColor(vp.tail)

            Assert.assertNotNull(h)
            Assert.assertNotNull(t)
            Assert.assertTrue(h != t)
        }
    }

    /**
     * Return a random association with index and a color string in RGB.
     */
    protected fun createColorMap(numColor: Int): Map<Int, String> {
        val colorCodes: MutableMap<Int, String> = HashMap()
        for (i in 0..99) {
            val rnd = Random(i.toLong())
            colorCodes[i] = String.format("\"#%2x%2x%2x\"", rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255))
        }
        return colorCodes
    }

    /**
     * Creates a list of integer colors.
     *
     * @param colorNumber number of colors
     * @return the list.
     */
    protected fun createColorsList(colorNumber: Int): Set<Int> {
        val colors: MutableSet<Int> = HashSet()
        for (j in 0..<colorNumber) {
            colors.add(j)
        }
        return colors
    }
}
